package net.toocruel.iqismart.entity.core;

import io.jsonwebtoken.lang.Collections;
import net.toocruel.iqismart.entity.base.BaseEntity;
import net.toocruel.iqismart.entity.common.Category;
import net.toocruel.iqismart.entity.common.Tag;
import net.toocruel.iqismart.entity.constants.ReviewStatus;
import net.toocruel.iqismart.entity.rbac.User;
import net.toocruel.iqismart.entity.validate.group.ControllerGroup;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.Set;

/**
 * @Description
 * @Auther sty
 * @createTime 2018/5/8 上午11:35
 */
@Entity
public class Question extends BaseEntity {
    private static final long serialVersionUID = -5308974945024766905L;
    @ManyToOne(cascade = CascadeType.DETACH,fetch = FetchType.LAZY,optional = false)
    @JoinColumn(name = "userId")
	private User user;
    private Long toUserId;
    @ManyToOne(cascade = CascadeType.DETACH,fetch = FetchType.EAGER)
    @JoinColumn(name = "categoryId")
    @NotNull(groups = ControllerGroup.class)
    private Category category;
    @Transient
    private Long categoryId;
    private String title;
    @Column(length = 10000)
    private String description;
    private int price;
    private Boolean hide = false;
    private int views;
    private int answerNum;
    private int supportNum;
    private byte device;
    private int status;
    private Long lastAnswer;


    @ManyToMany(cascade = CascadeType.DETACH,fetch = FetchType.LAZY)
    @JoinTable(name = "question_related",joinColumns = @JoinColumn(name = "questionId"),inverseJoinColumns = @JoinColumn(name = "relatedQuestionsId"))
    private Set<Question> relatedQuestions;

    @Transient
    private Boolean attented = false;

    @Transient
    private Boolean collected = false;

    @OneToMany(cascade = CascadeType.ALL,fetch = FetchType.LAZY,mappedBy = "question")
    @OrderBy("id desc")
    private Set<Answer> answers;
    @OneToOne(cascade = CascadeType.DETACH,fetch = FetchType.EAGER)
    @JoinColumn(name = "bestAnswerId")
    private Answer bestAnswer;

    @ManyToMany(cascade = CascadeType.DETACH,fetch = FetchType.LAZY)
    @JoinTable(name = "question_tag",joinColumns = @JoinColumn(name = "questionId"),inverseJoinColumns = @JoinColumn(name = "tagId"))
    private Set<Tag> tags;

    @OneToMany(cascade = CascadeType.DETACH,fetch = FetchType.LAZY,mappedBy = "question")
    private Set<Comment> comments;

    @OneToMany(cascade = CascadeType.DETACH,fetch = FetchType.LAZY,mappedBy = "question")
    private Set<Attention> attentions;

    //一对多  比如 一个收藏记录 只能对应一个问题
    @OneToMany(cascade = CascadeType.DETACH,fetch = FetchType.LAZY,mappedBy = "question")
    private Set<Collection> collections;


    //

    @Transient
    private Boolean answered;

    private Long syncedAt;

    public int getSupportNum() {
        return supportNum;
    }

    public void setSupportNum(int supportNum) {
        this.supportNum = supportNum;
    }

    public Long getSyncedAt() {
        return syncedAt;
    }

    public void setSyncedAt(Long syncedAt) {
        this.syncedAt = syncedAt;
    }

    public int getAnswerNum() {
        return answerNum;
    }

    public void setAnswerNum(int answerNum) {
        this.answerNum = answerNum;
    }

    public Long getCategoryId() {
        return categoryId;
    }

    public void setCategoryId(Long categoryId) {
        this.categoryId = categoryId;
    }

    public Set<Collection> getCollections() {
        return collections;
    }

    public Set<Question> getRelatedQuestions() {
        return relatedQuestions;
    }

    public void setRelatedQuestions(Set<Question> relatedQuestions) {
        this.relatedQuestions = relatedQuestions;
    }

    @Transient
    private String tagNames;


    public void setCollections(Set<Collection> collections) {
        this.collections = collections;
    }

    public Boolean getCollected() {
        return collected;
    }

    public void setCollected(Boolean collected) {
        this.collected = collected;
    }

    public Set<Attention> getAttentions() {
        return attentions;
    }

    public void setAttentions(Set<Attention> attentions) {
        this.attentions = attentions;
    }

    public Boolean getAttented() {
        return attented;
    }

    public void setAttented(Boolean attented) {
        this.attented = attented;
    }

    public Question() {
    }

    public String getTagNames() {
        StringBuffer sb = new StringBuffer();
        if(!Collections.isEmpty(tags)){
            tags.forEach(tag -> sb.append(tag.getName()).append(","));
            sb.deleteCharAt(sb.length()-1);
            return sb.toString();
        }
        return tagNames;
    }

    public void setTagNames(String tagNames) {
        this.tagNames = tagNames;
    }

    public Answer getBestAnswer() {
        return bestAnswer;
    }

    public void setBestAnswer(Answer bestAnswer) {
        this.bestAnswer = bestAnswer;
    }

    public Set<Tag> getTags() {
        return tags;
    }

    public void setTags(Set<Tag> tags) {
        this.tags = tags;
    }

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    public Long getToUserId() {
        return toUserId;
    }

    public void setToUserId(Long toUserId) {
        this.toUserId = toUserId;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Category getCategory() {
        return category;
    }

    public void setCategory(Category category) {
        if(category!=null && category.getId() > 0L){
            this.category = category;
        }

    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public Boolean getHide() {
        return hide;
    }

    public void setHide(Boolean hide) {
        this.hide = hide;
    }

    public int getViews() {
        return views;
    }

    public void setViews(int views) {
        this.views = views;
    }





    public byte getDevice() {
        return device;
    }

    public void setDevice(byte device) {
        this.device = device;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public Set<Answer> getAnswers() {
        return answers;
    }

    public void setAnswers(Set<Answer> answers) {
        this.answers = answers;
    }

    public Set<Comment> getComments() {
        return comments;
    }

    public void setComments(Set<Comment> comments) {
        this.comments = comments;
    }

    public Boolean getAnswered() {
        return answered;
    }

    public void setAnswered(Boolean answered) {
        this.answered = answered;
    }

    public Long getLastAnswer() {
        return lastAnswer;
    }

    public void setLastAnswer(Long lastAnswer) {
        this.lastAnswer = lastAnswer;
    }
}

